package model

import (
	"time"
	"errors"
)

type Section struct {
	Id         int64
	Duration   int32
	CreatedAt  time.Time   `xorm:"created"`
	UpdatedAt  time.Time   `xorm:"updated"`
	Qids       []int64
	Name       string
	TotalScore int32
	EveryScore float32
	Deduct     bool
	Group      int32
	Questions  []*Question `xorm:"-"`
}

func (s *Section) Some(ids []int64) ([]Section, error) {
	var sections []Section
	err := orm.In("id", ids).Find(&sections)
	return sections, err
}

func (s *Section) OneById() error {
	ok, err := orm.ID(s.Id).Get(s)
	if !ok {
		return errors.New(SectionNotFound)
	}
	return err
}

func (s *Section) Create() error {
	ok, err := orm.Insert(s)
	if ok == 0 {
		return errors.New(SectionCreateFailed)
	}
	return err
}

func (s *Section) Update() error {
	ok, err := orm.Id(s.Id).Cols("qids").Update(s)
	if ok == 0 {
		return errors.New(SectionUpdateFailed)
	}
	return err
}
